package com.woniuxy.project115.kaer.platform.util;

import org.lionsoul.ip2region.xdb.Searcher;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class IpUtil {
    private static final String DB_PATH = "D:\\woniuxueyuan\\四阶段项目\\KAER\\kaer-platform\\service\\src\\main\\resources\\ip2region.xdb"; // 更新路径
    private static Searcher searcher;

    static {
        try {
            Path path = Paths.get(DB_PATH);
            searcher = Searcher.newWithFileOnly(path.toFile().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getCityInfo(String ip) {
        try {
            String region = searcher.search(ip);
            System.out.println(region);
            return region.replace("|0", "").replace("0|", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
